
// mainWindow.cpp
#include "mainWindow.h"
#include "ui_mainWindow.h"  // 确保包含UI头文件
#include<QFile>
#include"qqcell.h"

MainWindow::MainWindow(QWidget *parent)
    : CustomMoveWidget(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setWindowFlags(Qt::FramelessWindowHint);

    qDebug() << "初始化按钮组...";

    m_btnGroup = new QButtonGroup(this);

    // 添加按钮到组

    // 检查按钮是否存在
    qDebug() << "btnFriend:" << ui->btnFriend;
    qDebug() << "btnGroup:" << ui->btnGroup;
    qDebug() << "btnConversation:" << ui->btnConversation;
    qDebug() << "btnApplay:" << ui->btnApplay;

    // 设置按钮为可选中（重要！）

    m_btnGroup = new QButtonGroup(this);
    m_btnGroup->addButton(ui->btnFriend, 0);
    m_btnGroup->addButton(ui->btnGroup, 2);
    m_btnGroup->addButton(ui->btnConversation, 1);
    m_btnGroup->addButton(ui->btnApplay, 3);
    ui->btnFriend->setCheckable(true);
    ui->btnGroup->setCheckable(true);
    ui->btnConversation->setCheckable(true);
    ui->btnApplay->setCheckable(true);
    // 检查按钮组连接
    connect(m_btnGroup, &QButtonGroup::idClicked, this, &MainWindow::SltMainPageChanged);

    connect(ui->btnmini,&QPushButton::clicked, this, &MainWindow::minclicked);
    connect(ui->btnWinClose, &QPushButton::clicked, this, &MainWindow::close);
    m_systemtray=new QSystemTrayIcon(this);
    connect(m_systemtray,&QSystemTrayIcon::activated,this,&MainWindow::clickedtray);
    menu=new QMenu(this);
    login=new QAction("上线");
    leave=new QAction("下线");
    disWod=new QAction("显示窗口");

    quit=new QAction("退出");
    menu->addAction(login);
    menu->addAction(leave);
    menu->addSeparator();
    menu->addAction(disWod);
    menu->addAction(quit);
    connect(disWod,&QAction::triggered,this,[this](){
        this->show();
    });
    connect(quit,&QAction::triggered,this,[this](){
        qApp->quit();
    });
    connect(login,&QAction::triggered,this,[this](){
        this->usersocket->CheckConnected();
    });
    connect(leave,&QAction::triggered,this,[this](){
        this->usersocket-> ColseConnected();
    });
    m_systemtray->setContextMenu(menu);

    // 设置默认选中
    ui->btnFriend->setChecked(true);
    InitSysMenu();
    InitQQListMenu();

    qDebug() << "初始化完成";
}
MainWindow::~MainWindow()
{
  delete ui;  // 正确删除
}

void MainWindow::SltMainPageChanged(int index)
{
    static int s_previousIndex = 0;

    qDebug() << "=== Page Change Debug ===";
    qDebug() << "Requested index:" << index;
    qDebug() << "Previous index:" << s_previousIndex;
    qDebug() << "Stacked widget count:" << ui->GCStackedWidget->count();
    //qDebug() << "Is animating:" << ui->GCStackedWidget->isAnimating(); // 需要添加此方法

    if (s_previousIndex == index) {
        qDebug() << "Same index, skipping";
        return;
    }

    if (index < 0 || index >= ui->GCStackedWidget->count()) {
        qDebug() << "Error: Index out of range";
        return;
    }

    // 设置动画
    AnimationStackedWidget::AnimationType direction =
        (index > s_previousIndex) ? AnimationStackedWidget::RightToLeft : AnimationStackedWidget::LeftToRight;

    qDebug() << "Animation direction:" << direction;

    ui->GCStackedWidget->setLength(ui->GCStackedWidget->width(), direction);
    ui->GCStackedWidget->start(index);

    s_previousIndex = index;
    qDebug() << "Page change completed";
}

void MainWindow::clickedtray(QSystemTrayIcon::ActivationReason reason)
{
    switch(reason)
    {
    case QSystemTrayIcon::Context://右击
        showmenu();
        break;
    case QSystemTrayIcon::DoubleClick:
        if(this->isHidden()) this->show();
        break;
    }
}
void MainWindow::minclicked()
{
    this->hide();
    QIcon icon=QIcon("://resource/background/app.png");
    m_systemtray->setIcon(icon);
    m_systemtray->setToolTip("托盘显示");
    m_systemtray->show();
}

void MainWindow::showmenu()
{
    menu->show();

}

void MainWindow::getsocket(ClientSocket *socket, QString name)
{
    usersocket=socket;
    username=name;
}
void MainWindow::InitSysMenu()
{
    // 设置子菜单
    QMenu *sysmenu = new QMenu(this);
    sysmenu->addAction(tr("系统设置"));
    sysmenu->addAction("消息管理器");
    sysmenu->addAction("文件助手");
    sysmenu->addSeparator();
    sysmenu->addAction("修改密码");
    sysmenu->addAction("帮助");
    sysmenu->addAction("连接服务器");
    sysmenu->addSeparator();
    sysmenu->addAction("升级");

    // 添加菜单
    ui->btnSysMenu->setMenu(sysmenu);
    connect(sysmenu, SIGNAL(triggered(QAction*)), this, SLOT(SltSysmenuCliecked(QAction*)));
}

/**
 * @brief MainWindow::InitQQListMenu
 * 添加好友列表菜单
 */
void MainWindow::InitQQListMenu()
{
    //设置菜单
    //设置组菜单
    QMenu *addFriend = new QMenu(this);
    addFriend->addAction(tr("添加好友"));
    addFriend->addAction(tr("刷新"));
    addFriend->addSeparator();
    addFriend->addAction(tr("删除该组"));
    connect(addFriend, SIGNAL(triggered(QAction*)), this, SLOT(onAddFriendMenuDidSelected(QAction*)));
    ui->frindListWidget->setGroupPopMenu(addFriend);

    // 设置子菜单
    QMenu *childMenu = new QMenu(this);
    childMenu->addAction(tr("发送即时消息"));
    childMenu->addSeparator();
    childMenu->addAction("移动至黑名单");
    childMenu->addAction("删除联系人");

    QMenu *groupListMenu = new QMenu(tr("移动联系人至"));
    childMenu->addMenu(groupListMenu);

    // childMenu->addAction(tr("创建讨论组"));
    connect(childMenu, SIGNAL(triggered(QAction*)), this, SLOT(onChildPopMenuDidSelected(QAction*)));
    ui->frindListWidget->setChildPopMenu(childMenu);

    //添加默认项
    //好友列表
    QQCell *myFriend = new QQCell;
    myFriend->groupName = QString(tr("我的好友"));
    myFriend->isOpen = false;
    myFriend->type = QQCellType_Group;
    myFriend->name = QString(tr("我的好友"));
    myFriend->subTitle = QString("(0/0)");
    ui->frindListWidget->insertQQCell(myFriend);


    QQCell *blacklist = new QQCell;
    blacklist->groupName = QString(tr("黑名单"));
    blacklist->isOpen = false;
    blacklist->type = QQCellType_Group;
    blacklist->name = QString(tr("黑名单"));
    blacklist->subTitle = QString("(0/0)");
    ui->frindListWidget->insertQQCell(blacklist);

    connect(ui->frindListWidget, SIGNAL(onChildDidDoubleClicked(QQCell*)), this, SLOT(SltFriendsClicked(QQCell*)));

    //组列表
    QMenu *myGroupMenu = new QMenu(this);
    myGroupMenu->addAction(tr("创建讨论组"));
    myGroupMenu->addAction(tr("加入讨论组"));
    myGroupMenu->addAction(tr("删除该组"));
    myGroupMenu->addSeparator();
    connect(myGroupMenu, SIGNAL(triggered(QAction*)), this, SLOT(onGroupPopMenuDidSelected(QAction*)));
    ui->groupListWidget->setGroupPopMenu(myGroupMenu);
    //设置子菜单
    QMenu *groupChildMen = new QMenu(this);
    groupChildMen->addAction(tr("发送即时消息"));
    ui->groupListWidget->setChildPopMenu(groupChildMen);

    //添加默认项
    QQCell *groupCell = new QQCell;
    groupCell->groupName = QString(tr("我的群组"));
    groupCell->isOpen = false;
    groupCell->type = QQCellType_GroupEx;
    groupCell->name = QString(tr("讨论组"));
    groupCell->subTitle = QString("(0/0)");
    ui->groupListWidget->insertQQCell(groupCell);

    connect(ui->groupListWidget, SIGNAL(onChildDidDoubleClicked(QQCell*)), this, SLOT(SltGroupsClicked(QQCell*)));
        // ... 你现有的分组创建代码 ...
        // 添加测试好友到"我的好友"分组
        //AddTestFriends();
}
// void MainWindow::AddTestFriends()
// {
//     // 创建好友1 - 在线
//     QQCell *friend1 = new QQCell;
//     friend1->type = QQCellType_Child;        // 重要：类型为好友
//     friend1->name = QString("张三");
//     friend1->subTitle = QString("忙碌中...");
//     friend1->iconPath = QString(":/resource/head/1.bmp");
//     friend1->id = 10001;
//     friend1->status = 1;  // 在线状态
//     friend1->groupName = QString("我的好友");  // 重要：指定所属分组
//     ui->frindListWidget->insertQQCell(friend1);

//     // 创建好友2 - 离线
//     QQCell *friend2 = new QQCell;
//     friend2->type = QQCellType_Child;
//     friend2->name = QString("李四");
//     friend2->subTitle = QString("离线");
//     friend2->iconPath = QString(":/resource/head/2.bmp");
//     friend2->id = 10002;
//     friend2->status = 0;  // 离线状态
//     friend2->groupName = QString("我的好友");
//     ui->frindListWidget->insertQQCell(friend2);

//     // 创建好友3 - 在线
//     QQCell *friend3 = new QQCell;
//     friend3->type = QQCellType_Child;
//     friend3->name = QString("王五");
//     friend3->subTitle = QString("在线");
//     friend3->iconPath = QString(":/resource/head/3.bmp");
//     friend3->id = 10003;
//     friend3->status = 1;
//     friend3->groupName = QString("我的好友");
//     ui->frindListWidget->insertQQCell(friend3);
// }

